export default {
  isEmpty(object) {
    if (object === null || object === undefined) {
      return false
    }
    for (var i in object) {
      // 存在属性或方法，则不是空对象
      return false
      // sodino.com
    }
    return true
  },
  IdentityCodeValid(code) {
    var city = {
      11: '北京',
      12: '天津',
      13: '河北',
      14: '山西',
      15: '内蒙古',
      21: '辽宁',
      22: '吉林',
      23: '黑龙江',
      31: '上海',
      32: '江苏',
      33: '浙江',
      34: '安徽',
      35: '福建',
      36: '江西',
      37: '山东',
      41: '河南',
      42: '湖北',
      43: '湖南',
      44: '广东',
      45: '广西',
      46: '海南',
      50: '重庆',
      51: '四川',
      52: '贵州',
      53: '云南',
      54: '西藏',
      61: '陕西',
      62: '甘肃',
      63: '青海',
      64: '宁夏',
      65: '新疆',
      71: '台湾',
      81: '香港',
      82: '澳门',
      91: '国外'
    }
    var tip = ''
    var pass = true
    if (!code || !/^\d{6}(18|19|20)?\d{2}(0[1-9]|1[12])(0[1-9]|[12]\d|3[01])\d{3}(\d|X)$/i.test(code)) {
      tip = '身份证号格式错误'
      pass = false
    } else if (!city[code.substr(0, 2)]) {
      tip = '地址编码错误'
      pass = false
    } else {
      if (code.length === 18) {
        code = code.split('')
        var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]
        // 校验位
        var parity = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']
        var sum = 0
        var ai = 0
        var wi = 0
        for (var i = 0; i < 17; i++) {
          ai = code[i]
          wi = factor[i]
          sum += ai * wi
        }
        var last = parity[sum % 11]
        if (last !== code[17]) {
          tip = '校验位错误'
          pass = false
        }
      }
    }
    if (!pass) return tip
  },
  validateAge(rule, value, callback) {
    if (!(/^[0-9]*$/).test(value)) {
      callback(new Error('请输入正确的年龄'))
      return
    }
    if (value > 100) {
      callback(new Error('请输入正确的年龄'))
    } else if (value <= 0) {
      callback(new Error('请输入正确的年龄'))
    }
    callback()
  },
  // 输入框长度小于16个字符，且必输
  validateStr(rule, value, callback) {
    if (value) {
      // Unicode编码
      var strlen = 0
      for (var i = 0; i < value.length; i++) {
        if (value.charCodeAt(i) > 255) { // 如果是汉字，则字符串长度加2
          strlen += 2
        } else {
          strlen++
        }
      }
      if (strlen > 16) {
        return callback(new Error('不能超过16字符'))
      } else {
        return callback()
      }
    } else {
      return callback(new Error('不能为空'))
    }
  },
  // 输入框长度小于16个字符,可不输入
  validateHasStr(rule, value, callback) {
    if (value) {
      // Unicode编码
      var strlen = 0
      for (var i = 0; i < value.length; i++) {
        if (value.charCodeAt(i) > 255) { // 如果是汉字，则字符串长度加2
          strlen += 2
        } else {
          strlen++
        }
      }
      if (strlen > 16) {
        return callback(new Error('不能超过16字符'))
      } else {
        return callback()
      }
    } else {
      return callback()
    }
  },
  createCSVStr(data, _outFields) {
    var textField = '"';
    var content = "";
    var len = 0,
      n = 0,
      comma = "",
      value = "";
    try {
      Array.forEach(_outFields, function (_field) {
        content = content + comma + _field;
        comma = ",";
      });

      content = content + "\r\n";
      len = data.length;
      n = _outFields.length;
      for (var i = 0; i < len; i++) {
        comma = "";
        for (var m = 0; m < n; m++) {
          var _field = _outFields[m];
          value = data[i][_field];
          if (!value && typeof value !== "number") {
            value = "";
          }
          if (value && /[",\r\n]/g.test(value)) {
            value = textField + value.replace(/(")/g, '""') + textField;
          }
          content = content + comma + value;
          comma = ",";
        }
        content = content + "\r\n";
      }
    } catch (err) {
      console.error(err);
      content = "";
    }

    return content;
  },
  exportCsv(obj, flieName) {
    //title ["","",""]
    var title = obj.title;
    //titleForKey ["","",""]
    var titleForKey = obj.titleForKey;
    var data = obj.data;
    var str = [];
    if (!!window.ActiveXObject || "ActiveXObject" in window) {
      var textField = '"';
      var content = "";
      var len = 0,
        n = 0,
        comma = "",
        value = "";
      try {
        Array.forEach(title, function (_field) {
          content = content + comma + _field;
          comma = ",";
        });
        content = content + "\r\n";
        len = data.length;
        n = titleForKey.length;
        for (var i = 0; i < len; i++) {
          comma = "";
          for (var m = 0; m < n; m++) {
            var _field = titleForKey[m];
            value = data[i][_field];
            if (!value && typeof value !== "number") {
              value = "";
            }
            if (value && /[",\r\n]/g.test(value)) {
              value = textField + value.replace(/(")/g, '""') + textField;
            }
            content = content + comma + value;
            comma = ",";
          }
          content = content + "\r\n";
        }
      } catch (err) {
        console.error(err);
        content = "";
      }
      console.log(content, 179)
      var BOM = "\uFEFF";
      var csvData = new Blob([BOM + content], { type: 'text/csv' });
      navigator.msSaveBlob(csvData, flieName + '.csv');
    } else {
      str.push(obj.title.join(",") + "\n");
      for (var i = 0; i < data.length; i++) {
        var temp = [];
        for (var j = 0; j < titleForKey.length; j++) {
          temp.push(data[i][titleForKey[j]]);
        }
        str.push(temp.join(",") + "\n");
      }
      var uri = 'data:text/csv;charset=utf-8,' + encodeURIComponent(str.join(""));
      var downloadLink = document.createElement("a");
      downloadLink.href = uri;
      downloadLink.download = flieName + '.csv';
      document.body.appendChild(downloadLink);
      downloadLink.click();
      document.body.removeChild(downloadLink);
    }
  },
  exportCsvByArray(obj, flieName) {
    var title = obj.title
    var data = obj.data
    var content = ''
    var len = 0, comma = ''
    try {
      Array.forEach(title, function (_field) {
        content = content + comma + _field
        comma = ','
      })
      content = content + '\r\n'
      len = data.length
      for (var i = 0; i < len; i++) {
        console.log(data[i], 241)
        var str = data[i].join(',') + '\n'
        content += str
      }
      console.log(content)
      if (!!window.ActiveXObject || "ActiveXObject" in window) {
        var BOM = "\uFEFF";
        var csvData = new Blob([BOM + content], { type: 'text/csv' });
        navigator.msSaveBlob(csvData, flieName + '.csv');
      } else {
        var uri = 'data:text/csv;charset=utf-8,' + encodeURIComponent(content);
        var downloadLink = document.createElement("a");
        downloadLink.href = uri;
        downloadLink.download = flieName + '.csv';
        document.body.appendChild(downloadLink);
        downloadLink.click();
        document.body.removeChild(downloadLink);
      }
    } catch (err) {
      console.error(err)
      content = ''
    }
  },
  uniqueArray(arr) {
    var res = [];
    var json = {};
    for (var i = 0; i < arr.length; i++) {
      if (!json[arr[i]]) {
        res.push(arr[i]);
        json[arr[i]] = 1;
      }
    }
    return res;
  }

  // ,
  //   validateStrLength(rule, value, callback, min, max) {
  //     console.log(rule)
  //     if (value.length > max) {
  //       callback(new Error('字符不能超过' + max + '个'))
  //     }
  //     if (value.length < min) {
  //       callback(new Error('字符不能小于' + min + '个'))
  //     }
  //   }
}
